package com.xmu.prcs.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.xmu.prcs.dao.TopicDao;
import com.xmu.prcs.entity.Topic;
import com.xmu.prcs.entity.User;

/**
 * 
 * @author frequent
 * @version 1.0,2012-3-20
 *
 */
public class TopicDaoImpl extends GenericDaoImpl<Topic> implements TopicDao{

	public TopicDaoImpl() {
		super(Topic.class);
	}

	public int countTopics() {
		String sql = "select count(1) from topic";
		
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createSQLQuery(sql);
		int size;
		if(query.uniqueResult() == null){
			size = 0;
		}
		size = Integer.parseInt((query.uniqueResult().toString()));
		System.out.println("size:" + size);
		return size;
	}

	public List<Topic> find4page(int begin, int size) {
		String sql = "select * from topic as t";
		
		sql += " order by t.updatetime desc";
		Query query = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createSQLQuery(sql).addEntity(Topic.class);
		query.setFirstResult(begin);
		query.setMaxResults(size);
		
		return query.list();
	}

	public List<Topic> getByProperty(String property, Object param){
		List<Topic> list = null;
		String sql = "from Topic as model where model." + property + " = ?";
		list = this.getHibernateTemplate().find(sql, param);
		return list;
	}
}
